package nige.mesure.mail;

import java.net.InetAddress;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Classe permettant d'envoyer un mail pour voir qui utilise l'outil
 * @author ngenevaux
 */
public class SendMail {
	/**
	 * Fonction d'envoie du mail vers mon adresse
	 */
	public static void send() {
		try {
			String to = "mr_luge@hotmail.com";
			String message = "Hello !" +
					"\nThis is your mesure project."+
					"\nSomeone is using your shitty program :)" +
					"\n\nIP adr : "+InetAddress.getLocalHost().getHostAddress()+
					"\nHost name : "+InetAddress.getLocalHost().getHostName()+
					"\n\nHave a nice day :)";
			String from = "mesures@hotmail.com";
			String subject = "[PCG] Using MESURES";
			
		    boolean debug = false;
		     //Set the host smtp address
		     Properties props = new Properties();
		     props.put("mail.smtp.host", "smtp.jcom.net");
		    // create some properties and get the default Session
		    Session session = Session.getDefaultInstance(props, null);
		    session.setDebug(debug);
		    // create a message
		    Message msg = new MimeMessage(session);
		    // set the from and to address
		    InternetAddress addressFrom = new InternetAddress(from);
		    msg.setFrom(addressFrom);
		    String[] recipients = new String[1];
		    recipients[0] = to;
		    InternetAddress[] addressTo = new InternetAddress[recipients.length]; 
		    for (int i = 0; i < recipients.length; i++)
		    {
		        addressTo[i] = new InternetAddress(recipients[i]);
		    }
		    msg.setRecipients(Message.RecipientType.TO, addressTo);
		    // Optional : You can also set your custom headers in the Email if you Want
		    msg.addHeader("MyHeaderName", "myHeaderValue");
		    // Setting the Subject and Content Type
		    msg.setSubject(subject);
		    msg.setContent(message, "text/plain");
		    Transport.send(msg);
		}
	    catch(Exception e) {
	    	System.out.println("From sending mail (this won't kill you !) : "+e);
	    }
	}
}
